local Register = class("Register", function()
    return require("ds/DataBase"):new()
end)
 
Register.evtConnectResponse = "RegisterConnectResponse"
Register.evtMainHallLogin = "MAIN_HALL_MSG_LOGIN"



function Register:init()
  self:onAddListener()
  
  return true
end

function Register:onAddListener()
    tools.Event:attach(ds.Register.evtConnectResponse, handler(self, self.onConnectResponse), "Ds_Register_Response")
    tools.Event:attach(ds.Register.evtMainHallLogin, handler(self, self.onMainHallLogin), "Ds_Register_MainHallLogin")
   
end

function Register:onRemoveListener()
    tools.Event:removeByEventNameAndObserverName(ds.Register.evtConnectResponse, "Ds_Register_Response")
    tools.Event:removeByEventNameAndObserverName(ds.Register.evtMainHallLogin, "Ds_Register_MainHallLogin")
    
end

function Register:destroy()
  self:onRemoveListener()
end

function Register:sendConnect(sIp , wPort )
    print("===sendConnect==="..sIp.."====wport=="..wPort)
    dv.LuaConnector:GetInstance(1):Connect(sIp,wPort)
end

function Register:onConnectResponse(wConnectID,wState)
    print("===OnConnectResponse===")
    dispatchEventCustom(ds.Register.evtConnectResponse, {wConnectID = wConnectID,wState = wState })
end

function Register:onMainHallLogin( wSubCmdID , packet )
    print("===Register:onMainHallLogin==wSubCmdID="..wSubCmdID)
   
    local structName
    if(wSubCmdID == Cmd.SUB_SC_USER_LOGIN_HALL_SUC ) then  -- 用户登录成功
       structName = 'CMD_SC_AgentUserLoginInfo'
    elseif(wSubCmdID == Cmd.SUB_SC_USER_LOGIN_HALL_FINISH ) then -- 用户登录完成 
       structName = 'CMD_SC_AgentUserLoginInfo'
    elseif(wSubCmdID == Cmd.SUB_SC_USER_LOGIN_HALL_FAIL ) then  -- 用户登录失败
       structName = 'CMD_SC_AgentUserLoginFail'
    end
    
    local structData = Struct:getInstance():getBodyData(structName)
    local data = Struct:getInstance():readBody(packet, structData)

    dispatchEventCustom(ds.Register.evtMainHallLogin, {wSubCmdID = wSubCmdID,  data = data } )
end


return Register
